import {getCookie, bindCaptchaBtn} from "./utils.js";

function resetPassword() {
    $('#reset-btn').on('click', function () {
        event.preventDefault();
        let email = $('#email').val();
        let captcha = $('#captcha').val();
        let password = $('#password').val();
        let password2 = $('#password2').val();
        if (!email) {
            alert('请输入邮箱');
            return;
        } else if (!captcha) {
            alert('请输入验证码');
            return;
        } else if (!password) {
            alert('请输入密码');
            return;
        }
        if (password !== password2) {
            alert('两次密码不一致');
            return;
        }
        const csrftoken = getCookie('csrftoken');
        $.ajax({
                url: '/auth/reset_password/',
                type: 'post',
                headers: {'X-CSRFToken': csrftoken},
                data: {
                    email: email,
                    captcha: captcha,
                    password: password,
                    password2: password2,
                },
                success: function (data) {
                    if (data.code === 200) {
                        alert('密码重置成功');
                        window.location.href = '/auth/login/';
                    } else
                        alert(data.message);

                },
                error: function () {
                    alert('网络错误');
                }
            }
        )
    })
}

$(function () {
    bindCaptchaBtn();
    resetPassword();
})